package com.sz.jobs;

import com.sz.dao.SetmealDao;
import com.sz.utils.QiNiuUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CleanImgJobs {

    private static final Logger log= LoggerFactory.getLogger(CleanImgJobs.class);

    @Autowired
    private SetmealDao setmealDao;

    public void Clean7NiuImgJob(){
        log.info("开始清理垃圾图片...");
        //查询七牛上所有图片
        List<String> ingIn7Niu = QiNiuUtils.listFile();
        log.info("七牛上共有{}张图片",ingIn7Niu.size());
        //查询数据库所有图片
        List<String> ingInDB = setmealDao.findImgs();
        log.info("数据库上共有{}张图片",ingInDB==null?0:ingInDB.size());
        //- 七牛上的图片集合减去数据库的图片
        ingIn7Niu.removeAll(ingInDB);
        log.info("需要清理七牛垃圾图片共{}张",ingIn7Niu.size());
        //调用七牛工具删除垃圾图片
        String[] array = ingIn7Niu.toArray(new String[]{});
        QiNiuUtils.removeFiles(array);
        log.info("清理七牛垃圾图片完成");
    }
}
